﻿/*
 * Copyright(C) 2012,ajayumi 保留所有权利。( All rights reserved. )
 * 
 * 文件名称：HttpHelper.cs
 * 摘    要：
 * 当前版本：1.0
 * 作    者：黄乙冬 (ajayumi)
 * 创建日期：2012年5月24日
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace ajayumi.Platform.Web
{
    /// <summary>
    /// HttpHelper.cs 网站开发辅助类，操作 Session、Application、Cookie 等对象
    /// Author   : 黄乙冬
    /// Date     : 2011-11-30
    /// </summary>
    public class HttpHelper
    {
        /// <summary>
        /// 保存对象到Session中
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="obj">要保存的对象</param>
        /// <param name="key">Session中的键名</param>
        public static void SetSession<T>(T obj, string key)
        {
            HttpContext.Current.Session[key] = obj;
        }

        /// <summary>
        /// 从Session中提取对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="key">Session中的键名</param>
        /// <returns>Session中的对象</returns>
        public static T GetSession<T>(string key)
        {
            T obj = default(T);
            if (HttpContext.Current.Session[key] != null)
            {
                obj = (T)HttpContext.Current.Session[key];
            }
            return obj;
        }

        /// <summary>
        /// 删除cookie值
        /// </summary>
        /// <param name="strName">名称</param>        
        public static void DelCookie(string strName)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
            if (cookie == null)
            {
                return;
            }
            cookie.Expires = DateTime.Now.AddDays(-300);

            HttpContext.Current.Response.Cookies.Set(cookie);

        }

        #region Write Cookie Methods

        /// <summary>
        /// 写cookie值，默认过期时间为1天
        /// </summary>
        /// <param name="cookieName">Cookeie名称</param>
        /// <param name="cookieValue">Cookeie值</param>
        public static void WriteCookie(string cookieName, string cookieValue)
        {
            WriteCookie(cookieName, cookieValue, 24 * 60);
        }
        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="cookieName">Cookeie名称</param>
        /// <param name="cookieValue">Cookeie值</param>
        /// <param name="expires">过期时间(分钟)</param>
        public static void WriteCookie(string cookieName, string cookieValue, int expires)
        {
            WriteCookie(cookieName, cookieValue, DateTime.Now.AddMinutes(expires));
        }
        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="cookieName">Cookeie名称</param>
        /// <param name="cookieValue">Cookeie值</param>
        /// <param name="expires">过期时间</param>
        public static void WriteCookie(string cookieName, string cookieValue, DateTime expires)
        {
            HttpContext.Current.Request.Cookies.Remove(cookieName);
            HttpCookie cookie = new HttpCookie(cookieName, cookieValue);
            cookie.Expires = expires;
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        #region Unused Method
        ///// <summary>
        ///// 写cookie值
        ///// </summary>
        ///// <param name="strName">cookie名称</param>
        ///// <param name="strKey">key值</param>
        ///// <param name="strValue">value值</param>
        //public static void WriteCookie(string strName, string strKey, string strValue)
        //{
        //    HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
        //    if (cookie == null)
        //    {
        //        cookie = new HttpCookie(strName);
        //        cookie.Values.Add(strKey, strValue);
        //    }
        //    else
        //    {
        //        cookie.Values.Set(strKey, strValue);
        //    }

        //    HttpContext.Current.Response.AppendCookie(cookie);
        //}
        ///// <summary>
        ///// 写cookie值
        ///// </summary>
        ///// <param name="strName">cookie名称</param>
        ///// <param name="strKey">key值</param>
        ///// <param name="strValue">value值</param>
        ///// <param name="expires">过期时间</param>        
        //public static void WriteCookie(string strName, string strKey, string strValue, int expires)
        //{
        //    HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
        //    if (cookie == null)
        //    {
        //        cookie = new HttpCookie(strName);
        //        cookie.Values.Add(strKey, strValue);
        //    }
        //    else
        //    {
        //        cookie.Values.Set(strKey, strValue);
        //    }
        //    cookie.Expires = DateTime.Now.AddMinutes(expires);

        //    HttpContext.Current.Response.AppendCookie(cookie);
        //}
        ///// <summary>
        ///// 写cookie值
        ///// </summary>
        ///// <param name="strName">cookie名称</param>
        ///// <param name="strKey">key值</param>
        ///// <param name="strValue">value值</param>
        ///// <param name="expires">过期时间</param>
        ///// <param name="domain">域名</param>
        //public static void WriteCookie(string strName, string strKey, string strValue, int expires, string domain)
        //{
        //    HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
        //    if (cookie == null)
        //    {
        //        cookie = new HttpCookie(strName);
        //        cookie.Values.Add(strKey, strValue);
        //    }
        //    else
        //    {
        //        cookie.Values.Set(strKey, strValue);
        //    }
        //    cookie.Expires = DateTime.Now.AddMinutes(expires);
        //    cookie.Domain = domain;

        //    HttpContext.Current.Response.AppendCookie(cookie);

        //}
        #endregion
        
        #endregion

        #region Read Cookie Methods

           /// <summary>
        /// 读cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <returns>cookie值</returns>
        public static string ReadCookie(string strName)
        {
            if (HttpContext.Current.Request.Cookies != null &&
                HttpContext.Current.Request.Cookies[strName] != null)
            {
                return HttpContext.Current.Request.Cookies[strName].Value.ToString();
            }

            return string.Empty;
        }

        /// <summary>
        /// 读cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <param name="strKey">key名称</param>
        /// <returns>cookie值</returns>
        public static string ReadCookie(string strName, string strKey)
        {
            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null && HttpContext.Current.Request.Cookies[strName][strKey] != null)
            {
                return HttpContext.Current.Request.Cookies[strName][strKey].ToString();
            }

            return string.Empty;
        }

        #endregion

     
    }
}
